/*
 * 文件名： CategoryFragment.java
 * 描    述：分类Fragment
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.miguo.activity.base.BaseFragment;
import com.miguo.activity.callback.IFragment;
import com.miguo.app.AppConfig;
import com.miguo.app.AppHelper;
import com.miguo.bean.CategoryBean;
import com.miguo.component.CategoryView;
import com.miguo.db.CategoryDB;
import com.miguo.framework.component.BaseStatusView;
import com.miguo.http.Url;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;
import com.miguo.utils.Category;
import com.miguo.utils.LongUtils;

import java.util.List;

public class CategoryFragment extends BaseFragment implements OnClickListener, IFragment {

    private View view;
    private CategoryView mCategoryView;
    private BaseStatusView mBaseStatus;
    private LinearLayout mSearchll;

    private List<CategoryBean> mCategoryList;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initCategory();
    }

    private void initView() {
        mCategoryView = (CategoryView) view.findViewById(R.id.category_view);

        view.findViewById(R.id.category_baoyou_rl).setOnClickListener(this);
        view.findViewById(R.id.category_fengding_rl).setOnClickListener(this);
        view.findViewById(R.id.category_xinpin_rl).setOnClickListener(this);
        view.findViewById(R.id.category_yugao_rl).setOnClickListener(this);

        mBaseStatus = (BaseStatusView) view.findViewById(R.id.base_status_view);
        mBaseStatus.setContentView(mCategoryView);
        mBaseStatus.getRefreshView().setOnClickListener(this);
        mBaseStatus.getNetworkView().setOnClickListener(this);

        mSearchll = (LinearLayout) view.findViewById(R.id.category_search_ll);
        mSearchll.setOnClickListener(this);
    }

    private void initCategory() {
        mCategoryList = CategoryDB.getInstance(mContext, true).findPid(LongUtils.StrToLong("0"));
        if (mCategoryList.size() >= 1) {
            mBaseStatus.exit(true);
            mCategoryView.setList(mCategoryList);
        } else {
            Category.getInstance(mContext, true).init();
            mBaseStatus.setRefreshVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.refresh_rl:
                initCategory();
                break;
            case R.id.network_error_ll:
                initCategory();
                break;
            case R.id.category_search_ll:
                AppHelper.StartSearch(mActivity, null);
                break;
            case R.id.category_baoyou_rl:
                AppHelper.StartItem(mActivity, "九块九包邮", "1", Url.toRoute(Url.Jiu.List, 1));
                break;
            case R.id.category_fengding_rl:
                AppHelper.StartItem(mActivity, "20元封顶", "2", Url.toRoute(Url.Jiu.List, 1));
                break;
            case R.id.category_xinpin_rl:
                AppHelper.StartItem(mActivity, "今日新品", "", Url.toRoute(Url.Item.Xinpin, 1));
                break;
            case R.id.category_yugao_rl:
                AppHelper.StartYuGao(mActivity);
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_category, container, false);
        return view;
    }

    @Override
    public void onStartFragment() {
        Statistics.onTrackCustomClick(mContext, AppConfig.STATISTICS.EVENT_NAME_2);
    }

    @Override
    public void onStopFragment() {

    }
}
